package cn.lili.controller.order;

import cn.lili.common.aop.annotation.PreventDuplicateSubmissions;
import cn.lili.common.security.AuthUser;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.common.utils.ResultUtil;
import cn.lili.common.utils.StringUtils;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.order.order.entity.dos.OrderFlow;
import cn.lili.modules.order.order.entity.dto.OrderFlowQueryDTO;
import cn.lili.modules.order.order.entity.dto.OrderItemFlowSearchParams;
import cn.lili.modules.order.order.entity.enums.OrderFlowStatusEnum;
import cn.lili.modules.order.order.entity.enums.OrderItemAfterSaleStatusEnum;
import cn.lili.modules.order.order.entity.params.DirectPushOrderParams;
import cn.lili.modules.order.order.entity.vo.*;
import cn.lili.modules.order.order.service.OrderFlowService;
import cn.lili.modules.order.order.service.OrderFlowStatisticsService;
import cn.lili.modules.order.order.service.OrderIntegrationService;
import cn.lili.modules.order.order.service.OrderItemFlowService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Objects;

/**
 * 订单流水控制器
 *
 * @author liushuai(liushuai711 @ gmail.com)
 * @version v4.0
 * @Description:
 * @since 2023/4/14 16:39
 */
@RestController
@RequestMapping("/order/flow")
@Api(tags = "订单流水API")
@RequiredArgsConstructor
public class OrderFlowController {

    private final OrderFlowService orderFlowService;

    private final OrderItemFlowService orderItemFlowService;

    private final OrderFlowStatisticsService orderFlowStatisticsService;

    private final OrderIntegrationService orderIntegrationService;

    @ApiOperation(value = "分页获取订单流水")
    @GetMapping
    public ResultMessage<Page<OrderFlow>> page(OrderFlowQueryDTO queryDTO) {
        AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser());
        if (SceneEnums.STORE.equals(currentUser.getScene())) {
            queryDTO.setStoreId(currentUser.getExtendId());
        } else if (SceneEnums.SUPPLIER.equals(currentUser.getScene())) {
            queryDTO.setSupplierId(currentUser.getExtendId());
        }
        return ResultUtil.data(orderFlowService.orderFlowPage(queryDTO));
    }

    @ApiOperation(value = "分页获取订单流水")
    @GetMapping("/statistics")
    public ResultMessage<OrderFlowStatisticsVO> statistics(OrderFlowQueryDTO queryDTO) {
        return ResultUtil.data(orderFlowStatisticsService.staticsByQueryParams(queryDTO));
    }

    @ApiOperation(value = "分页获取分销订单流水")
    @GetMapping("/distribution/mine")
    public ResultMessage<Page<OrderItemFlowUserVO>> mineItemFlow(OrderItemFlowSearchParams searchParams) {
        AuthUser currentUser = Objects.requireNonNull(UserContext.getCurrentUser());
        searchParams.setDistributionId(currentUser.getExtendId());
        searchParams.setAfterSaleStatus(OrderItemAfterSaleStatusEnum.EXPIRED.name());
        return ResultUtil.data(orderItemFlowService.getOrderFlowUserVOByCondition(searchParams));
    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "待审核流水")
    @GetMapping(value = "/waitVerify")
    public ResultMessage<Page<OrderFlow>> waitVerify(PageVO pageVO, String orderSn, String orderFlowStatus) {

        if (StringUtils.isEmpty(orderFlowStatus)) {
            orderFlowStatus = OrderFlowStatusEnum.AUDIT.name();
        }

        return ResultUtil.data(orderFlowService.waitVerify(pageVO, orderSn, orderFlowStatus));
    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "相关流水查询：根据某一订单sn查询所有流水信息")
    @GetMapping(value = "/other")
    public ResultMessage<List<OrderFlow>> listByOrderSn(String orderSn) {
        return ResultUtil.data(orderFlowService.listByOrderSn(orderSn));
    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "管理员强制所有端审核通过，让订单继续执行")
    @PostMapping(value = "/force")
    public ResultMessage<Object> force(String orderSn) {
        orderIntegrationService.force(orderSn);
        return ResultUtil.success();
    }

    @ApiOperation(value = "获取订单流水详情")
    @GetMapping("/{id}")
    public ResultMessage<OrderFlow> detail(@PathVariable String id) {
        OrderFlow orderFlow = orderFlowService.orderFlowDetail(id);
        return ResultUtil.data(orderFlow);
    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "审核通过")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "流水ID", required = true, dataType = "String", paramType = "path")
    })
    @PostMapping(value = "/verify/{id}")
    public ResultMessage<Object> verify(@NotBlank(message = "订单编号不能为空") @PathVariable String id) {
        orderIntegrationService.pass(id);
        return ResultUtil.success();
    }

    @PreventDuplicateSubmissions
    @ApiOperation(value = "审核拒绝")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "流水ID", required = true, dataType = "String", paramType = "path")
    })
    @PostMapping(value = "/refuse/{id}")
    public ResultMessage<Object> refuse(@PathVariable String id) {
        orderIntegrationService.refuse(id);
        return ResultUtil.success();
    }

    /**
     * 我的收益
     */
    @GetMapping("/myEarnings")
    public ResultMessage<Object> myEarnings() {
        return ResultUtil.data(orderFlowService.myEarnings());
    }

    /**
     * 收益记录
     * @param incomeRecordReq 类型
     * @return 收益记录
     */
    @GetMapping("/incomeRecord")
    public ResultMessage<Object> incomeRecord(IncomeRecordReq incomeRecordReq) {
        return orderFlowService.incomeRecord(incomeRecordReq);
    }


    /**
     * 直销业绩列表
     * @param dto
     * @return
     */
    @GetMapping("/getLowerOrderFlowPriceByDisMemberId")
    @ApiOperation("直销业绩列表")
    public ResultMessage<OrderFlowVO> getOrderFlowsByDisMemberId(OrderFlowQueryDTO dto) {
        if (StringUtils.isEmpty(dto.getDistributionMemberId())) {
            return ResultUtil.error(400, "店主id不可为空");
        }
        return ResultUtil.data(orderFlowService.getOrderFlowsByDisMemberId(dto));
    }

    /**
     * 佣金收益列表
     * @param dto
     * @return
     */
    @GetMapping("/getTotalCommissByDisMemberId")
    @ApiOperation("佣金收益列表")
    public ResultMessage<OrderFlowVO> getTotalCommissByDisMemberId(OrderFlowQueryDTO dto) {
        if (StringUtils.isEmpty(dto.getDistributionMemberId())) {
            return ResultUtil.error(400, "店主id不可为空");
        }
        return ResultUtil.data(orderFlowService.getTotalCommissByDisMemberId(dto));
    }

    /**
     * 直销订单列表
     */
    @GetMapping("/directPushOrderList")
    public ResultMessage<Page<IncomeRecordVO>> directPushOrderList(DirectPushOrderParams pushOrderParams) {
        return ResultUtil.data(orderFlowService.directPushOrderList(pushOrderParams));
    }

    /**
     * 区域直推订单总金额
     */
    @GetMapping("/directPushOrderMoney")
    public ResultMessage<Object> directPushOrderMoney(DirectPushOrderParams pushOrderParams) {
        return ResultUtil.data(orderFlowService.directPushOrderMoney(pushOrderParams));
    }

    /**
     * 预备城主 + 当前城主
     */
    @GetMapping("/premaster")
    public ResultMessage<Object> premaster(@RequestParam String regionId) {
        return ResultUtil.data(orderFlowService.premaster(regionId));
    }

    /**
     * 查询当前区域下的订单流水统计
     *
     * @param regionId 区域ID
     */
    @GetMapping("/orderFlowCompute")
    public ResultMessage<Object> orderFlowCompute(@RequestParam String regionId) {
        return ResultUtil.data(orderFlowService.orderFlowCompute(regionId, null, null));
    }

    /**
     * 查询当前区域下的订单
     *
     * @param regionId 区域ID
     */
    @GetMapping("/orderFlowPage")
    public ResultMessage<Object> orderFlowPage(@RequestParam String regionId, Integer isUnion, Integer type, PageVO pageVO) {
        return ResultUtil.data(orderFlowService.orderFlowPage(regionId, isUnion, type, pageVO));
    }

    /**
     * 城主历史收益
     * @return 城主历史收益
     */
    @GetMapping("/historyEarnings")
    public ResultMessage<Object> historyEarnings(PageVO pageVO) {
        return ResultUtil.data(orderFlowService.historyEarnings(pageVO));
    }

}
